package org.game.quartz;

import com.xiaoleilu.hutool.date.DateUtil;
import org.game.cache.OnlinePlayerCache;
import org.game.db.bean.Player;
import org.game.module.datingbo.trigger.CityDatingTriggerManager;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 城市约会
 * Created by fxf on 2017/11/9.
 */
public class InviteDatingJob implements Job {

    static final Log log = LogFactory.get(InviteDatingJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
    	try {
            Map<String,List<Integer>> triggerTimeFrame = CityDatingTriggerManager.getTimeFrame();
            Date now =  context.getScheduledFireTime();
    		Collection<Player> players = OnlinePlayerCache.getAll();
            int hour = DateUtil.thisHour(true);
            players.forEach(player -> {
                CityDatingTriggerManager.refreshCityDating(player,now);
                if (!CityDatingTriggerManager.getJustRefreshHour().contains(hour)){
                    CityDatingTriggerManager.trigger(player, triggerTimeFrame,now);
                }

            });
		} catch (Exception e) {
			log.error(e);
		}
    }

}
